package com.open4a.openstudy.activity.splash;

import android.os.AsyncTask;
import android.os.Bundle;

import com.open4a.openstudy.R;
import com.open4a.openstudy.activity.main.MainActivity;
import com.open4a.openstudy.activity.oauth.OauthActivity;
import com.open4a.openstudy.manager.oauth.OauthManager;

import common.base.BaseActivity;

public class SplashActivity extends BaseActivity {

    private static final int SPLASH_DURATION = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        SplashAsyncTask task = new SplashAsyncTask();
        task.execute();
    }

    private class SplashAsyncTask extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected Boolean doInBackground(Void... params) {
            try {
                Thread.sleep(SPLASH_DURATION);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return OauthManager.getInstance().isAuth();
        }

        @Override
        protected void onPostExecute(Boolean isAuth) {
            if (isAuth) {
                startActivity(MainActivity.class);
            } else {
                startActivity(OauthActivity.class);
            }
        }
    }
}
